﻿using MengXunAppTool.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Windows.Documents;
using System.Xml.Linq;

namespace MengXunAppTool.DAL
{
    public class VerDAL
    {
        private Sql3Helper sqlHelper = new Sql3Helper();
        public bool Check()
        {
            return sqlHelper.SqlCheck();
        }
        public bool IsDuplicate(Sql3Helper.MX_AppVerRecord model)
        {
            return sqlHelper.db.Queryable<Sql3Helper.MX_AppVerRecord>()
                .Where(it => it.Customer == model.Customer && it.Product == model.Product&&it.AppName==model.AppName&&it.Version==model.Version)
                .Count() >0;
        }
        public List<Sql3Helper.MX_AppVerRecord> GetAllApp(bool isQE=false, string name = "")
        {
            var list = sqlHelper.db.Queryable<Sql3Helper.MX_AppVerRecord>()
                 .WhereIF(isQE == false  , it => it.Enable == "True" && (string.IsNullOrEmpty(it.CheckUser1) || string.IsNullOrEmpty(it.CheckTime) == false))
                 .WhereIF(isQE == true, it => it.CheckUser1 == name && it.Enable == "True" && (string.IsNullOrEmpty(it.CheckUser1) == false && string.IsNullOrEmpty(it.CheckTime)))
                 .ToList()
                 .OrderBy(it => it.Customer)
                 .ThenBy(it => it.Product)
                 .ThenBy(it => it.AppName)
                 .ThenByDescending(it => it.Version)
                 .ToList();
            List<Sql3Helper.MX_AppVerRecord> apps = new List<Sql3Helper.MX_AppVerRecord>();
            if (isQE)
            {
                foreach (var item in list)
                {
                    var last = GetLastApp(item.Customer, item.Product, item.AppName);
                    if (last.Version == item.Version)
                    {
                        apps.Add(item);
                    }
                }
                return apps;
            }
            else
            {
                return list;
            }
        }
        public List<string> GetCustomers()
        {
            return sqlHelper.db.Queryable<Sql3Helper.MX_AppVerRecord>()
                 .GroupBy(it => new { it.Customer })
                 .OrderBy(it => it.Customer)
                 .Select(it => it.Customer)
                 .ToList();
        }
        public List<string> GetCustomers(string enable, string name = "")
        {
            return sqlHelper.db.Queryable<Sql3Helper.MX_AppVerRecord>()
             .Where(it => it.Enable == enable)
             .WhereIF(string.IsNullOrEmpty(name) == false, it => it.CheckUser1 == name && string.IsNullOrEmpty(it.CheckTime))
             .GroupBy(it => new { it.Customer })
             .OrderBy(it => it.Customer)
             .Select(it => it.Customer)
             .ToList();
        }
        public List<string> GetProducts(string customer)
        {
            return sqlHelper.db.Queryable<Sql3Helper.MX_AppVerRecord>()
             .Where(it => it.Customer == customer)
             .GroupBy(it => new { it.Product })
             .OrderBy(it => it.Product)
             .Select(it => it.Product)
             .ToList();
        }
        public List<string> GetProducts(string customer,string enable,string name="")
        {
            return sqlHelper.db.Queryable<Sql3Helper.MX_AppVerRecord>()
             .Where(it => it.Customer == customer && it.Enable == enable)
            .WhereIF(string.IsNullOrEmpty(name) == false, it => it.CheckUser1 == name && string.IsNullOrEmpty(it.CheckTime))
            .GroupBy(it => new { it.Product })
            .OrderBy(it => it.Product)
             .Select(it => it.Product)
             .ToList();
        }
        public List<string> GetAppNames(string customer, string product)
        {
            return sqlHelper.db.Queryable<Sql3Helper.MX_AppVerRecord>()
             .Where(it => it.Customer == customer && it.Product == product)
             .GroupBy(it => new { it.AppName })
             .OrderBy(it => it.AppName)
             .Select(it => it.AppName)
             .ToList();
        }
        public List<string> GetAppNames(string customer, string product, string enable)
        {
            return sqlHelper.db.Queryable<Sql3Helper.MX_AppVerRecord>()
             .Where(it => it.Customer == customer && it.Product == product && it.Enable == enable)
             .GroupBy(it => new { it.AppName })
             .OrderBy(it => it.AppName)
             .Select(it => it.AppName)
             .ToList();
        }
        public string GetLastVer(string customer,string product, string name)
        {
            Sql3Helper.MX_AppVerRecord appVerRecord = sqlHelper.db.Queryable<Sql3Helper.MX_AppVerRecord>()
                .Where(it => it.Customer == customer &&it.Product==product&& it.AppName == name)
                .OrderBy(it=>it.ReleaseTime,SqlSugar.OrderByType.Desc).First();
            if (appVerRecord == null)
            {
                return "V1.0";
            }
            else
            {
                return appVerRecord.Version;
            }
        }
        public string GetLastVer(string customer, string product, string name,string enable)
        {
            Sql3Helper.MX_AppVerRecord appVerRecord = sqlHelper.db.Queryable<Sql3Helper.MX_AppVerRecord>()
                .Where(it => it.Customer == customer && it.Product == product && it.AppName == name&&it.Enable==enable)
                .OrderBy(it => it.ReleaseTime, SqlSugar.OrderByType.Desc)
                .First();
            if (appVerRecord == null)
            {
                return "V1.0";
            }
            else
            {
                return appVerRecord.Version;
            }
        }
        public Sql3Helper.MX_AppVerRecord GetLastApp(string customer, string product, string name)
        {
            return sqlHelper.db.Queryable<Sql3Helper.MX_AppVerRecord>()
                .Where(it => it.Customer == customer && it.Product == product && it.AppName == name)
                .OrderBy(it => it.ReleaseTime, SqlSugar.OrderByType.Desc).First();
        }
        public List<string> GetVer(string customer, string product, string name)
        {
            return sqlHelper.db.Queryable<Sql3Helper.MX_AppVerRecord>()
                .Where(it => it.Customer == customer && it.Product == product && it.AppName == name)
                .OrderBy(it => it.ReleaseTime, SqlSugar.OrderByType.Asc)
                .Select(it=>it.Version)
                .ToList();
        }
        public Sql3Helper.MX_AppVerRecord GetApp(string customer, string product, string name,string ver)
        {
            return sqlHelper.db.Queryable<Sql3Helper.MX_AppVerRecord>().
                Where(it => it.Customer == customer && it.Product == product && it.AppName == name&&it.Version==ver).
                OrderBy(it => it.ReleaseTime, SqlSugar.OrderByType.Desc).First();
        }
        public List<Sql3Helper.MX_AppVerRecord> GetApps()
        {
            return sqlHelper.db.Queryable<Sql3Helper.MX_AppVerRecord>().ToList();
        }
        public Sql3Helper.MX_AppVerRecord GetApp(int id)
        {
            return sqlHelper.db.Queryable<Sql3Helper.MX_AppVerRecord>().
                Where(it => it.id==id).First();
        }
        public DateTime GetLastReleaseTime(string name)
        {
            Sql3Helper.MX_AppVerRecord appVerRecord = sqlHelper.db.Queryable<Sql3Helper.MX_AppVerRecord>().
                Where(it => it.AppName == name).
                OrderBy(it => it.ReleaseTime, SqlSugar.OrderByType.Desc).First();
            if (appVerRecord == null)
            {
                return new DateTime();
            }
            else
            {
                return appVerRecord.ReleaseTime;
            }
        }
        public bool InsertMX_AppVerRecord(Sql3Helper.MX_AppVerRecord mX_Ver)
        {
            mX_Ver.ReleaseTime = sqlHelper.db.GetDate();
            return sqlHelper.db.Insertable(mX_Ver).ExecuteCommand() == 1;
        }
        public bool ChangeMX_AppVerRecord(Sql3Helper.MX_AppVerRecord mX_Ver)
        {
            return sqlHelper.db.Updateable(mX_Ver).ExecuteCommand() == 1;
        }
        public bool DelMX_AppVerRecord(int id)
        {
            return sqlHelper.db.Deleteable<Sql3Helper.MX_AppVerRecord>().Where(it=>it.id==id).ExecuteCommand() == 1;
        }
    }
}
